package com.woniu.humanresource.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.woniu.humanresource.entity.AttendanceStyle;
import com.woniu.humanresource.dao.AttendanceStyleMapper;
import com.woniu.humanresource.entity.Interviewer;
import com.woniu.humanresource.listener.AttendanceStyleListener;
import com.woniu.humanresource.listener.InterviewerListener;
import com.woniu.humanresource.service.AttendanceStyleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyq
 * @since 2022-07-11
 */
@Service
public class AttendanceStyleServiceImpl extends ServiceImpl<AttendanceStyleMapper, AttendanceStyle> implements AttendanceStyleService {
    @Resource
    AttendanceStyleMapper attendanceStyleMapper;

    @Value("${upload.addResourceLocation2}")
    private String addResourceLocation;
    @Value("${download.url2}")
    private String downloadUrl;
    @Override
    public void importAttendanceStyle(MultipartFile attendanceStyleExcel) {

        //        上传文件创建文件路径
        DateFormat dateFormat=new SimpleDateFormat("yyyy.MM.dd");
        String dateStr = dateFormat.format(new Date());
        File file1 = new File(addResourceLocation+"attendanceStyleExcel."+dateStr);
        if(!file1.exists()){
            file1.mkdirs();
        }
        String path=addResourceLocation+"attendanceStyleExcel."+dateStr;
        File file = new File(path, UUID.randomUUID()+attendanceStyleExcel.getOriginalFilename());
        try {
            attendanceStyleExcel.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
//            读取文件数据
        /**
         * 第一个参数为，从哪获取文件路径
         * 第二个参数为实体类.class，
         */
        ExcelReaderBuilder workBook = EasyExcel.read(file.getPath(), AttendanceStyle.class, new AttendanceStyleListener());
        ExcelReaderSheetBuilder sheet = workBook.sheet();
        sheet.doRead();
//

        List<AttendanceStyle> attendanceStyleList = AttendanceStyleListener.getAttendanceStyleList();
        for(AttendanceStyle attendanceStyle:attendanceStyleList){
            attendanceStyleMapper.insert(attendanceStyle);
        }
    }

    @Override
    public void exportAttendanceStyle(HttpServletResponse response) {
        List<AttendanceStyle> attendanceStyles = attendanceStyleMapper.selectList(null);
        try {
            ExcelWriterBuilder workBook = EasyExcel.write(downloadUrl, AttendanceStyle.class);
            workBook.sheet().doWrite(attendanceStyles);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
